/*
 * FirestoreIndex api apends __name__ as an item to the
 * end of the fields list if not present. We are suppressing
 * this server generated field.
 */
func firestoreIFieldsDiffSuppressFunc(k, old, new string, d TerraformResourceDataChange) bool {
	kLength := "fields.#"
	oldLength, newLength := d.GetChange(kLength)
	oldInt, ok := oldLength.(int)
	if !ok {
		return false
	}
	newInt, ok := newLength.(int)
	if !ok {
		return false
	}

	if oldInt == newInt+1 {
		kold := fmt.Sprintf("fields.%v.field_path", oldInt-1)
		knew := fmt.Sprintf("fields.%v.field_path", newInt-1)

		oldLastIndexName, _ := d.GetChange(kold)
		_, newLastIndexName := d.GetChange(knew)
		if oldLastIndexName == "__name__" && newLastIndexName != "__name__" {
			oldBase := fmt.Sprintf("fields.%v", oldInt-1)
			if strings.HasPrefix(k, oldBase) || k == kLength {
				return true
			}
		}
	}
	return false
}

func firestoreIFieldsDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
	return firestoreIFieldsDiffSuppressFunc(k, old, new, d)
}